<?php

namespace App\Kuafu\Shop\Controller\Merchant\Outside;

use App\Exceptions\BaseException;
use App\Exceptions\CustomValidateException;
use App\Kuafu\Shop\Model\CameraModel;
use App\Kuafu\Shop\Service\CameraImportService;
use App\Kuafu\Shop\Service\DeviceService;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\Shop\Validate\DeviceOutsideValidate;
use App\Kuafu\Shop\Validate\DeviceValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use App\Kuafu\Base\Controller\BaseController;
use Illuminate\Support\Facades\Cache;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * 摄像头
 */
class DeviceOutsideController extends BaseController
{

    /**
     * 接口作废 - 勿用
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getMQTTAccount(Request $request)
    {
        $serialNo    = $request->get('serialNo');
        $accountList = Cache::remember('device:getMQTTAccount:cache:' . $serialNo, 86400, function () use ($serialNo) {
            return ShopService::getMQTTAccountByDeviceSerialNo($serialNo);
        });
        return $this->success($accountList);
    }

    /**
     * 接口作废 - 勿用
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getDeviceConfig(Request $request)
    {
        $serialNo = $request->get('serialNo');

        $config = Cache::remember('device:getDeviceConfig:cache:' . $serialNo, 86400, function () use ($serialNo) {
            return DeviceService::getDeviceConfig($serialNo);
        });

        return $this->success($config);
    }


    /**
     * 获取设备绑定情况 + MQ + 设备信息 + 素材版本
     *
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function getDeviceInfo(Request $request): JsonResponse
    {
        $serialNo = $request->get('serialNo');

        $deviceInfo = DeviceService::getDeviceRow($serialNo);

        if (!$deviceInfo) {
            return $this->fail(400001, '设备尚未绑定');
        }

        // 获取设备配置信息
        $config = DeviceService::getDeviceConfigCache($serialNo);

        // 获取设备的 MQ 账号信息
        $mqAccount = ShopService::getMQTTAccountByDeviceSerialNo($serialNo)['shop'];

        $result = [
            'config'    => $config,
            'mqAccount' => $mqAccount,
            'version'   => $deviceInfo->updatedAt->timestamp
        ];

        return $this->success($result);
    }

    /**
     * 获取设备绑定的素材
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getDeviceMaterial(Request $request): JsonResponse
    {
        $serialNo = $request->get('serialNo');
        $version  = $request->get('version', '0');

        $deviceInfo = DeviceService::getDeviceRow($serialNo);

        if (!$deviceInfo) {
            return $this->fail(400001, '设备尚未绑定');
        }

        $data = Cache::remember('device:getDeviceMaterial:cache:' . $serialNo, 86400, function () use (
            $serialNo
        ) {
            return DeviceService::deviceAdv($serialNo);
        });

        return $this->success($data);
    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public function mergeImageFace(Request $request): JsonResponse
    {
        $params = DeviceOutsideValidate::mergeImageFaceValidate($request->all());
        return $this->success(DeviceService::mergeImageFace($params));
    }

    /**
     * 模板列表
     *
     * @return JsonResponse
     * @throws BaseException
     */
    public function mergeImageTemplate(): JsonResponse
    {
        return $this->success(DeviceService::mergeImageTemplate());
    }
}
